package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "菜品相关接口")
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     * @param dishPageQuery
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQuery){
        log.info("菜品分页查询: {}", dishPageQuery);
        PageResult pageResult = dishService.pageQuery(dishPageQuery);
        return Result.success(pageResult);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")//新增菜品，将该 所在“分类”从redis中清空
    @ApiOperation(("新增菜品"))
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品: {}", dishDTO);
        dishService.save(dishDTO);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品: {}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",allEntries = true)//修改菜品可能影响多个分类，所以清空所有缓存
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品: {}", dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteBatch(@RequestParam List<Long> ids){
        log.info("删除菜品: {}", ids);
        dishService.deleteBatch(ids);
        return Result.success() ;
    }

    /**
     * 批量起售停售
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @PostMapping("/status/{status}")
    @ApiOperation("设置菜品状态")
    public Result StartOrStop(@PathVariable Integer status, Long id){
        log.info("设置菜品状态: {}, {}", status, id);
        dishService.StartOrStop(status, id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类ID查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类ID查询菜品: {}", categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}
